﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class SubstanceManager : AbpZeroTemplateDomainServiceBase
    {

        public IRepository<Product, int> productRepository { get; private set; }

        public SubstanceManager(IRepository<Product, int> _productRepository)
        {
            productRepository = _productRepository;
        }

        public int SetSubstanceProduct(Abp.Mtrl.Material substance, bool hiddenNo, Product product)
        {
            if (substance.Id > 0)
            {

                new Abp.Mtrl.MtrlHelper().Update(substance);
            }
            else
            {
               

                new Abp.Mtrl.MtrlHelper().Save(substance);
            }

            if (MolService.ServiceClient != null)
            {
                MolService.ServiceClient.Append2Temporary(substance.Id.ToString(), substance.MolString);
            }


            if (!hiddenNo)
            {
                if (product.Id == 0)
                {
                    if (productRepository.GetAll().Any(x => x.Code == product.Code))
                    {
                        throw new Abp.UI.UserFriendlyException("系统提示", string.Format("编号{0}已存在", product.Code));
                    }
                    var p = new Product()
                    {
                        Code = product.Code,
                        SubstanceId = substance.Id
                    };
                    productRepository.Insert(p);
                    product.Id = p.Id;
                }
                else
                {
                    if (productRepository.GetAll().Any(x => x.Code == product.Code && x.Id != product.Id))
                    {
                        throw new Abp.UI.UserFriendlyException("系统提示", string.Format("编号{0}已存在", product.Code));
                    }
                    var p = productRepository.Get(product.Id);
                    p.Code = product.Code;
                    p.SubstanceId = substance.Id;
                }
            }

            return substance.Id;

        }


        public List<Abp.Mtrl.Material> PageList(string cas, string cnname, string enname, string keyword,
            string sort, int start, int limit, out int total)
        {

            return new Abp.Mtrl.MtrlHelper().PageList(cas, cnname, enname, keyword, sort, start, limit, out total);

        }

        public List<Abp.Mtrl.Material> Query(List<string> cas, List<string> cnname, List<string> enname, List<string> formula)
        {
            return new Abp.Mtrl.MtrlHelper().Query(cas, cnname, enname, formula);
        }

        public Abp.Mtrl.Material Get(int Id)
        {
            return new Abp.Mtrl.MtrlHelper().Get(Id);
        }

        public Abp.Mtrl.Material Save(Abp.Mtrl.Material sub)
        {
            new Abp.Mtrl.MtrlHelper().Save(sub);
            if (MolService.ServiceClient != null)
            {
                MolService.ServiceClient.Append2Temporary(sub.Id.ToString(), sub.MolString);
            }

            return sub;
        }

        public Abp.Mtrl.Material Update(Abp.Mtrl.Material sub)
        {
            new Abp.Mtrl.MtrlHelper().Update(sub);
            if (MolService.ServiceClient != null)
            {
                MolService.ServiceClient.Append2Temporary(sub.Id.ToString(), sub.MolString);
            }
            return sub;
        }

    }
}
